package com.nado.iims2.blog.util;

import java.lang.Character.UnicodeBlock;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * The Class CommonUtil.
 * 
 * @author Lixing
 *
 */
public class CommonUtil {
	/** The log. */
	private static Log log = LogFactory.getLog(CommonUtil.class);
	
	/**
	 * Calc offset.
	 * 
	 * @param page the page
	 * @param pageSize the page size
	 * @return the int
	 */
	public static int calcOffset(int page, int pageSize) {
		return (page - 1) * pageSize;
	}

	/**
	 * Trunc string for80 byte.
	 * 
	 * @param oriMsg the ori msg
	 * @return the string
	 */
	public static String truncStringFor80Byte(String oriMsg) {

		int maxByte = 80;
		int nowByte = 0;
		String smsLimitMsg = "";

		if (oriMsg != null) {
			for (int i = 0; i < oriMsg.length(); i++) {

				UnicodeBlock uniChar = Character.UnicodeBlock.of(oriMsg.charAt(i));

				if (uniChar == Character.UnicodeBlock.HANGUL_SYLLABLES || uniChar == Character.UnicodeBlock.HANGUL_COMPATIBILITY_JAMO) { //한글인지 판단
					nowByte += 2;
				} else if (uniChar == Character.UnicodeBlock.BASIC_LATIN) { //그외 1byte(영문,숫자,기호)인지 판단
					nowByte += 1;
				} else { //그외 특문
					nowByte += 2;
				}
				smsLimitMsg += oriMsg.charAt(i);

				if (nowByte > maxByte) {
					smsLimitMsg = smsLimitMsg.substring(0, smsLimitMsg.length() - 1);
					break;
				}
			}
		}

		return smsLimitMsg;
	}

	/**
	 * Checks if is longer than80 byte.
	 * 
	 * @param oriMsg the ori msg
	 * @return true, if is longer than80 byte
	 */
	public static boolean isLongerThan80Byte(String oriMsg) {

		int maxByte = 80;
		int nowByte = 0;
		String smsLimitMsg = "";
		boolean smsLimitYn = false;

		if (oriMsg != null) {
			for (int i = 0; i < oriMsg.length(); i++) {

				UnicodeBlock uniChar = Character.UnicodeBlock.of(oriMsg.charAt(i));

				if (uniChar == Character.UnicodeBlock.HANGUL_SYLLABLES || uniChar == Character.UnicodeBlock.HANGUL_COMPATIBILITY_JAMO) { //한글인지 판단
					nowByte += 2;
				} else if (uniChar == Character.UnicodeBlock.BASIC_LATIN) { //그외 1byte(영문,숫자,기호)인지 판단
					nowByte += 1;
				} else { //그외 특문
					nowByte += 2;
				}
				smsLimitMsg += oriMsg.charAt(i);

				if (nowByte > maxByte) {
					smsLimitYn = true;
					break;
				}
			}
		}

		return smsLimitYn;
	}

	/**
	 * 심플하게 XSS를 필터링하는 유틸.
	 * 
	 * @param str str
	 * @return 필터링된 파라미터
	 * @throws Exception Exception
	 */
	public static String xssSimpleFilter(String str) throws Exception {
		log.debug("\n\n\nXSS Simple Filter Checking");
		log.debug("Original Paramter : " + str);

		String localStr = str;

		if (localStr != null) {
			//localStr = localStr.replaceAll(";", "&#59;");
			localStr = localStr.replaceAll("&", "&amp;");
			localStr = localStr.replaceAll("#", "&#35;");
			localStr = localStr.replaceAll("%", "&#37;");
			localStr = localStr.replaceAll("\"", "&quot;");
			localStr = localStr.replaceAll("<", "&lt;");
			localStr = localStr.replaceAll(">", "&gt;");
			localStr = localStr.replaceAll("'", "&#39;");
			localStr = localStr.replaceAll("document", "");
			localStr = localStr.replaceAll("alert", "");
			localStr = localStr.replaceAll("cookie", "");
			localStr = localStr.replaceAll("scrip", "");
		}

		log.debug("Filtered Parameter : " + localStr);
		log.debug("\n\n\n");
		return localStr;
	}

	/**
	 * 소수점 반올림.
	 * 
	 * @param infval the infval
	 * @param step the step
	 * @return the float
	 * @return
	 */
	public static float roundDecimal(float infval, int step) {
		return (float)(Math.round(infval * Math.pow(10, step)) / (Math.pow(10, step)));
	}

	/**
	 * Checks if is belong to0 between10.
	 * 
	 * @param value the value
	 * @return true, if is belong to0 between10
	 */
	public static boolean isBelongTo0Between10(double value) {
		if (0 <= value && value <= 10) {
			return true;
		} else {
			return false;
		}
	}

	/**
	 * 마지막으로 일치하는 string replacement.
	 * 
	 * @param ori the ori
	 * @param oStr the o str
	 * @param nStr the n str
	 * @return the string
	 */
	public static String replaceLast(String ori, String oStr, String nStr) {
		String result = "";

		if (StringUtils.isNotEmpty(ori)) {
			result = StringUtils.substring(ori, 0, StringUtils.lastIndexOf(ori, oStr));
			result += nStr;
		}

		return result;
	}
	
	public static String formatContent(String content) {
        Pattern pattern = Pattern.compile("(\r\n)\\1{1,}");   
        Matcher matcher = pattern.matcher(content);   
		
        return matcher.replaceAll("\n");
	}
}
